<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="/themes/admin/assets/css/layui.css">
    <script src="/themes/admin/assets/layui.js"></script>
    <script src="/themes/admin/assets/jquery-3.2.1.min.js"></script>
  </head>
  <body>
    <table class="layui-table">
      <colgroup>
        <col width="150">
        <col width="200">
        <col>
      </colgroup>
      <thead>
        <tr>          
          <th>插件名</th>
          <th>作者</th>
          <th>版本</th>
          <th>简介</th>
          <th>管理</th>
        </tr> 
      </thead>
      <tbody>
        {volist name="pluginList" id="vo"}
        <tr>        
          <td>{$vo.title}</td>
          <td>{$vo.author}</td>
          <td>{$vo.version}</td>
          <td>{$vo.description}</td>
          <td>
            <div id="{$vo.name}" class="layui-btn-group"></div>
          </td>
        </tr>
        <script>
          if('{$vo.status}' == 0){
            $('#'+'{$vo.name}').html('<button class="layui-btn" onclick=\'install("{$vo.name}")\'>安装</button><button class="layui-btn layui-btn-danger" onclick=\'pluginDelete("{$vo.name}")\'>删除</button>');
          }else if('{$vo.status}' == 1){
            $("#{$vo.name}").html('<button class="layui-btn layui-btn-normal" onclick=\'pluginOpen("{$vo.name}")\'>启用</button><button onclick=\'uninstall("{$vo.name}")\' class="layui-btn layui-btn-primary">卸载</button>');
          }else if('{$vo.status}' == 2){
            $("#{$vo.name}").html('<button class="layui-btn layui-btn-normal" onclick=\'pluginStop("{$vo.name}")\'>停用</button><a href="{:addon_url($vo.name .'://Admin/index')}" class="layui-btn layui-btn-normal">设置</a>');
          }
        </script>
        {/volist}
      </tbody>
    </table>
    <script>
      layui.use('layer');
      //安装插件
      function install(pluginName){
        $.ajax({
          type:"post",
          url:"/admin/plugin/install",
          data:"name="+pluginName,
          success:function(e){
            if(e.code == 1){
              $("#"+pluginName).html('<button class="layui-btn layui-btn-normal" onclick=\'pluginOpen('+'"'+pluginName+'"'+')\'>启用</button><button onclick=\'uninstall("{$vo.name}")\' class="layui-btn layui-btn-primary">卸载</button>');
              layer.msg(e.msg);
            }else{
              layer.msg(e.msg);
            }
          }
        });
      }
      //卸载插件
      function uninstall(pluginName){
        $.ajax({
          type:"post",
          url:"/admin/plugin/uninstall",
          data:"name="+pluginName,
          success:function(e){
            if(e.code == 1){
              $('#'+pluginName).html('<button class="layui-btn" onclick=\'install('+'"'+pluginName+'"'+')\'>安装</button><button onclick=\'pluginDelete("{$vo.name}")\' class="layui-btn layui-btn-danger">删除</button>');
              layer.msg(e.msg);
            }else{
              layer.msg(e.msg);
            }
          }
        });
      }
      //插件启用
      function pluginOpen(pluginName){
        $.ajax({
          type:"post",
          url:"/admin/plugin/open",
          data:"name="+pluginName,
          success:function(e){
            if(e.code == 1){
              $("#"+pluginName).html('<button class="layui-btn layui-btn-normal"  onclick=\'pluginStop('+'"'+pluginName+'"'+')\'>停用</button><a href="/addons/execute/'+pluginName+'-admin-index.html" class="layui-btn layui-btn-normal">设置</a>');
              layer.msg(e.msg);
            }else{
              layer.msg(e.msg);
            }
          }
        });
      }
      //插件停用
      function pluginStop(pluginName){
        $.ajax({
          type:"post",
          url:"/admin/plugin/stop",
          data:"name="+pluginName,
          success:function(e){
            if(e.code == 1){
              $("#"+pluginName).html('<button class="layui-btn layui-btn-normal" onclick=\'pluginOpen('+'"'+pluginName+'"'+')\'>启用</button><button onclick=\'uninstall("{$vo.name}")\' class="layui-btn layui-btn-primary">卸载</button>');
              layer.msg(e.msg);
            }else {
              layer.msg(e.msg);
            }
          }
        });
      }
      //插件删除
      function pluginDelete(pluginName){
        $.ajax({
          type:"post",
          url:"/admin/plugin/delete",
          data:"name="+pluginName,
          success:function(e){
            if(e.code == 1){
              $("#"+pluginName).parents("tr").remove();
              layer.msg(e.msg);
            }else{
              layer.msg(e.msg);
            }
          }
        });
      }
    </script>
  </body>
</html>